package ai.moran.test.header;

import com.rabbitmq.client.*;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author haitao
 */
public class Producter {
    public static void main(String[] args) throws Exception {
        //1 创建一个ConnectionFactory, 并进行配置
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("10.80.20.209");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("abm-order");
        connectionFactory.setUsername("dc_shop");
        connectionFactory.setPassword("JF8tga3#s3");


        //2 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        //3 通过connection创建一个Channel
        Channel channel = connection.createChannel();

        String routingKey = "";
        String exchange ="test_headers_exchange";

        Map<String, Object> map = new Hashtable<>();
        map.put("user", "wyt");
        map.put("inform_type", "sms");
        AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
        properties.headers(map);

        //4 通过Channel发送数据
        for (int i = 0; i < 5; i++) {
            String msg = "Hello RabbitMQ!";
            //1 exchange   2 routingKey  3 附加属性  4 消息体
            channel.basicPublish(exchange, routingKey,properties.build(), msg.getBytes());
        }

        //5 记得要关闭相关的连接
        channel.close();
        connection.close();
    }

}
